﻿using System;
using UnityEngine;

namespace RootMotion.FinalIK
{
	// Token: 0x02000035 RID: 53
	[Serializable]
	public class IKMapping
	{
		// Token: 0x06000278 RID: 632 RVA: 0x00003A3D File Offset: 0x00001C3D
		public virtual bool IsValid(IKSolver solver, ref string message)
		{
			return true;
		}

		// Token: 0x06000279 RID: 633 RVA: 0x000028E7 File Offset: 0x00000AE7
		public virtual void Initiate(IKSolverFullBody solver)
		{
		}

		// Token: 0x0600027A RID: 634 RVA: 0x00025364 File Offset: 0x00023564
		public bool BoneIsValid(Transform bone, IKSolver solver, ref string message, Warning.Logger logger = null)
		{
			if (bone == null)
			{
				message = "IKMappingLimb contains a null reference.";
				if (logger != null)
				{
					logger(message);
				}
				return false;
			}
			if (solver.GetPoint(bone) == null)
			{
				message = "IKMappingLimb is referencing to a bone '" + bone.name + "' that does not excist in the Node Chain.";
				if (logger != null)
				{
					logger(message);
				}
				return false;
			}
			return true;
		}

		// Token: 0x0600027B RID: 635 RVA: 0x00023DB8 File Offset: 0x00021FB8
		public Vector3 SolveFABRIKJoint(Vector3 pos1, Vector3 pos2, float length)
		{
			return pos2 + (pos1 - pos2).normalized * length;
		}

		// Token: 0x02000036 RID: 54
		[Serializable]
		public class BoneMap
		{
			// Token: 0x0600027D RID: 637 RVA: 0x00003A80 File Offset: 0x00001C80
			public void Initiate(Transform transform, IKSolverFullBody solver)
			{
				this.transform = transform;
				solver.GetChainAndNodeIndexes(transform, out this.chainIndex, out this.nodeIndex);
			}

			// Token: 0x17000028 RID: 40
			// (get) Token: 0x0600027E RID: 638 RVA: 0x00003A9C File Offset: 0x00001C9C
			public Vector3 swingDirection
			{
				get
				{
					return this.transform.rotation * this.localSwingAxis;
				}
			}

			// Token: 0x0600027F RID: 639 RVA: 0x00003AB4 File Offset: 0x00001CB4
			public void StoreDefaultLocalState()
			{
				this.defaultLocalPosition = this.transform.localPosition;
				this.defaultLocalRotation = this.transform.localRotation;
			}

			// Token: 0x06000280 RID: 640 RVA: 0x00003AD8 File Offset: 0x00001CD8
			public void FixTransform(bool position)
			{
				if (position)
				{
					this.transform.localPosition = this.defaultLocalPosition;
				}
				this.transform.localRotation = this.defaultLocalRotation;
			}

			// Token: 0x17000029 RID: 41
			// (get) Token: 0x06000281 RID: 641 RVA: 0x00003B02 File Offset: 0x00001D02
			public bool isNodeBone
			{
				get
				{
					return this.nodeIndex != -1;
				}
			}

			// Token: 0x06000282 RID: 642 RVA: 0x00003B10 File Offset: 0x00001D10
			public void SetLength(IKMapping.BoneMap nextBone)
			{
				this.length = Vector3.Distance(this.transform.position, nextBone.transform.position);
			}

			// Token: 0x06000283 RID: 643 RVA: 0x00003B33 File Offset: 0x00001D33
			public void SetLocalSwingAxis(IKMapping.BoneMap swingTarget)
			{
				this.SetLocalSwingAxis(swingTarget, this);
			}

			// Token: 0x06000284 RID: 644 RVA: 0x00003B3D File Offset: 0x00001D3D
			public void SetLocalSwingAxis(IKMapping.BoneMap bone1, IKMapping.BoneMap bone2)
			{
				this.localSwingAxis = Quaternion.Inverse(this.transform.rotation) * (bone1.transform.position - bone2.transform.position);
			}

			// Token: 0x06000285 RID: 645 RVA: 0x00003B75 File Offset: 0x00001D75
			public void SetLocalTwistAxis(Vector3 twistDirection, Vector3 normalDirection)
			{
				Vector3.OrthoNormalize(ref normalDirection, ref twistDirection);
				this.localTwistAxis = Quaternion.Inverse(this.transform.rotation) * twistDirection;
			}

			// Token: 0x06000286 RID: 646 RVA: 0x000253CC File Offset: 0x000235CC
			public void SetPlane(IKSolverFullBody solver, Transform planeBone1, Transform planeBone2, Transform planeBone3)
			{
				this.planeBone1 = planeBone1;
				this.planeBone2 = planeBone2;
				this.planeBone3 = planeBone3;
				solver.GetChainAndNodeIndexes(planeBone1, out this.plane1ChainIndex, out this.plane1NodeIndex);
				solver.GetChainAndNodeIndexes(planeBone2, out this.plane2ChainIndex, out this.plane2NodeIndex);
				solver.GetChainAndNodeIndexes(planeBone3, out this.plane3ChainIndex, out this.plane3NodeIndex);
				this.UpdatePlane(true, true);
			}

			// Token: 0x06000287 RID: 647 RVA: 0x00025434 File Offset: 0x00023634
			public void UpdatePlane(bool rotation, bool position)
			{
				Quaternion lastAnimatedTargetRotation = this.lastAnimatedTargetRotation;
				if (rotation)
				{
					this.defaultLocalTargetRotation = QuaTools.RotationToLocalSpace(this.transform.rotation, lastAnimatedTargetRotation);
				}
				if (position)
				{
					this.planePosition = Quaternion.Inverse(lastAnimatedTargetRotation) * (this.transform.position - this.planeBone1.position);
				}
			}

			// Token: 0x06000288 RID: 648 RVA: 0x00003B9C File Offset: 0x00001D9C
			public void SetIKPosition()
			{
				this.ikPosition = this.transform.position;
			}

			// Token: 0x06000289 RID: 649 RVA: 0x00003BAF File Offset: 0x00001DAF
			public void MaintainRotation()
			{
				this.maintainRotation = this.transform.rotation;
			}

			// Token: 0x0600028A RID: 650 RVA: 0x00003BC2 File Offset: 0x00001DC2
			public void SetToIKPosition()
			{
				this.transform.position = this.ikPosition;
			}

			// Token: 0x0600028B RID: 651 RVA: 0x00025498 File Offset: 0x00023698
			public void FixToNode(IKSolverFullBody solver, float weight, IKSolver.Node fixNode = null)
			{
				if (fixNode == null)
				{
					fixNode = solver.GetNode(this.chainIndex, this.nodeIndex);
				}
				if (weight >= 1f)
				{
					this.transform.position = fixNode.solverPosition;
					return;
				}
				this.transform.position = Vector3.Lerp(this.transform.position, fixNode.solverPosition, weight);
			}

			// Token: 0x0600028C RID: 652 RVA: 0x00003BD5 File Offset: 0x00001DD5
			public Vector3 GetPlanePosition(IKSolverFullBody solver)
			{
				return solver.GetNode(this.plane1ChainIndex, this.plane1NodeIndex).solverPosition + this.GetTargetRotation(solver) * this.planePosition;
			}

			// Token: 0x0600028D RID: 653 RVA: 0x00003C05 File Offset: 0x00001E05
			public void PositionToPlane(IKSolverFullBody solver)
			{
				this.transform.position = this.GetPlanePosition(solver);
			}

			// Token: 0x0600028E RID: 654 RVA: 0x00025500 File Offset: 0x00023700
			public void RotateToPlane(IKSolverFullBody solver, float weight)
			{
				Quaternion quaternion = this.GetTargetRotation(solver) * this.defaultLocalTargetRotation;
				if (weight >= 1f)
				{
					this.transform.rotation = quaternion;
					return;
				}
				this.transform.rotation = Quaternion.Lerp(this.transform.rotation, quaternion, weight);
			}

			// Token: 0x0600028F RID: 655 RVA: 0x00003C19 File Offset: 0x00001E19
			public void Swing(Vector3 swingTarget, float weight)
			{
				this.Swing(swingTarget, this.transform.position, weight);
			}

			// Token: 0x06000290 RID: 656 RVA: 0x00025558 File Offset: 0x00023758
			public void Swing(Vector3 pos1, Vector3 pos2, float weight)
			{
				Quaternion quaternion = Quaternion.FromToRotation(this.transform.rotation * this.localSwingAxis, pos1 - pos2) * this.transform.rotation;
				if (weight >= 1f)
				{
					this.transform.rotation = quaternion;
					return;
				}
				this.transform.rotation = Quaternion.Lerp(this.transform.rotation, quaternion, weight);
			}

			// Token: 0x06000291 RID: 657 RVA: 0x000255D0 File Offset: 0x000237D0
			public void Twist(Vector3 twistDirection, Vector3 normalDirection, float weight)
			{
				Vector3.OrthoNormalize(ref normalDirection, ref twistDirection);
				Quaternion quaternion = Quaternion.FromToRotation(this.transform.rotation * this.localTwistAxis, twistDirection) * this.transform.rotation;
				if (weight >= 1f)
				{
					this.transform.rotation = quaternion;
					return;
				}
				this.transform.rotation = Quaternion.Lerp(this.transform.rotation, quaternion, weight);
			}

			// Token: 0x06000292 RID: 658 RVA: 0x00003C2E File Offset: 0x00001E2E
			public void RotateToMaintain(float weight)
			{
				if (weight <= 0f)
				{
					return;
				}
				this.transform.rotation = Quaternion.Lerp(this.transform.rotation, this.maintainRotation, weight);
			}

			// Token: 0x06000293 RID: 659 RVA: 0x00025648 File Offset: 0x00023848
			public void RotateToEffector(IKSolverFullBody solver, float weight)
			{
				if (!this.isNodeBone)
				{
					return;
				}
				float num = weight * solver.GetNode(this.chainIndex, this.nodeIndex).effectorRotationWeight;
				if (num <= 0f)
				{
					return;
				}
				if (num >= 1f)
				{
					this.transform.rotation = solver.GetNode(this.chainIndex, this.nodeIndex).solverRotation;
					return;
				}
				this.transform.rotation = Quaternion.Lerp(this.transform.rotation, solver.GetNode(this.chainIndex, this.nodeIndex).solverRotation, num);
			}

			// Token: 0x06000294 RID: 660 RVA: 0x000256E8 File Offset: 0x000238E8
			public Quaternion GetTargetRotation(IKSolverFullBody solver)
			{
				Vector3 solverPosition = solver.GetNode(this.plane1ChainIndex, this.plane1NodeIndex).solverPosition;
				Vector3 solverPosition2 = solver.GetNode(this.plane2ChainIndex, this.plane2NodeIndex).solverPosition;
				Vector3 solverPosition3 = solver.GetNode(this.plane3ChainIndex, this.plane3NodeIndex).solverPosition;
				if (solverPosition == solverPosition3)
				{
					return Quaternion.identity;
				}
				return Quaternion.LookRotation(solverPosition2 - solverPosition, solverPosition3 - solverPosition);
			}

			// Token: 0x1700002A RID: 42
			// (get) Token: 0x06000295 RID: 661 RVA: 0x00025764 File Offset: 0x00023964
			public Quaternion lastAnimatedTargetRotation
			{
				get
				{
					if (this.planeBone1.position == this.planeBone3.position)
					{
						return Quaternion.identity;
					}
					return Quaternion.LookRotation(this.planeBone2.position - this.planeBone1.position, this.planeBone3.position - this.planeBone1.position);
				}
			}

			// Token: 0x0400019F RID: 415
			public Transform transform;

			// Token: 0x040001A0 RID: 416
			public int chainIndex = -1;

			// Token: 0x040001A1 RID: 417
			public int nodeIndex = -1;

			// Token: 0x040001A2 RID: 418
			public Vector3 defaultLocalPosition;

			// Token: 0x040001A3 RID: 419
			public Quaternion defaultLocalRotation;

			// Token: 0x040001A4 RID: 420
			public Vector3 localSwingAxis;

			// Token: 0x040001A5 RID: 421
			public Vector3 localTwistAxis;

			// Token: 0x040001A6 RID: 422
			public Vector3 planePosition;

			// Token: 0x040001A7 RID: 423
			public Vector3 ikPosition;

			// Token: 0x040001A8 RID: 424
			public Quaternion defaultLocalTargetRotation;

			// Token: 0x040001A9 RID: 425
			public Quaternion maintainRotation;

			// Token: 0x040001AA RID: 426
			public float length;

			// Token: 0x040001AB RID: 427
			public Quaternion animatedRotation;

			// Token: 0x040001AC RID: 428
			public Transform planeBone1;

			// Token: 0x040001AD RID: 429
			public Transform planeBone2;

			// Token: 0x040001AE RID: 430
			public Transform planeBone3;

			// Token: 0x040001AF RID: 431
			public int plane1ChainIndex = -1;

			// Token: 0x040001B0 RID: 432
			public int plane1NodeIndex = -1;

			// Token: 0x040001B1 RID: 433
			public int plane2ChainIndex = -1;

			// Token: 0x040001B2 RID: 434
			public int plane2NodeIndex = -1;

			// Token: 0x040001B3 RID: 435
			public int plane3ChainIndex = -1;

			// Token: 0x040001B4 RID: 436
			public int plane3NodeIndex = -1;
		}
	}
}
